--
-- Author: yawei.zhu
-- Date: 2018-10-12 15:01:25
--
-- GoddessWishBaseRender
--

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.GoddessWishBaseNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._tipEnable = false
    self._showOrign = false

    -- 兼容 多层节点优化
    self._panel = self:getChildByName('Panel')
    if not self._panel then
        self._panel = self
    else
        self:setContentSize(self._panel:getContentSize())
    end

    display.uiAddClick(self._panel, function(sender, eventType)
            self:showTip()
        end)

    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        local type = self._data.itemBagType
        local data = self._data

        self:setLevel()
        self:setNumBgVisible(false)
        if type == Enums.ItemBagType.daibi then
            self:setIcon(data:getIcon())
            self:setNum(data:getAmount())
        elseif type == Enums.ItemBagType.mount then
            self:setNum(1)
            self:setIcon(data:getIcon())
        elseif type == Enums.ItemBagType.partner then --随从
            self:setNum(1)
            self:setIcon(data:getTipsIcon())
        elseif type == Enums.ItemBagType.fashion then --时装
            self:setNum(1)
            self:setIcon(data:getIcon())
        elseif type == Enums.ItemBagType.equip or type == Enums.ItemBagType.item then
            self:setIcon(data:getIcon())
            self:setNum(data:getAmount())

            if self._data:getItemType() == Enums.ItemType.crystal then
                --如果是水晶 设置显示等级
                local rockInfo = GD:queryRockById(self._data:getId())
                if rockInfo then
                    self:setLevel(tonumber(rockInfo.leve))
                end
            end
        elseif type == Enums.ItemBagType.jewel then -- 宝石不显示数量？？？
            self:setIcon(data:getIcon())
            self:setLevelEx(data:getLevelPlus1())
        elseif type == Enums.ItemBagType.fame then
            self:setIcon(data:getIcon())
            self:setNum(data:getAmount())
        elseif type == Enums.ItemBagType.mountSoul then
            self:setIcon(data:getIcon())
            self:setNum(data:getAmount())
        elseif type == Enums.ItemBagType.mapBuff then
            self:setIcon(data:getIcon())
            self:setNum(data:getAmount())
        else
            if data then
                self:setIcon(data.getIcon and data:getIcon())
                self:setNum(data.getAmount and data:getAmount() or 0)
            else
                self:setNum(0)
            end
            -- print("IconRender2")
        end
    end
end

function ClassRef:setIcon(res)
    self._ui.icon:setVisible(true)
    self._ui.icon:loadTexture(res)
end

function ClassRef:setNumBgVisible(b)
    self._ui.Panel_num:setVisible(b)
end

function ClassRef:setLevel(level)
    if level and level > 0 then
        self._ui.level:setVisible(true)
        self._ui.level:setString("+"..tostring(level))
    else
        self._ui.level:setVisible(false)
    end
end

function ClassRef:setLevelEx(level)
    if level and level > 0 then
        self._ui.level:setVisible(true)
        self._ui.level:setString(tostring(level)) -- 去掉了+号
    else
        self._ui.level:setVisible(false)
    end
end

function ClassRef:setNum(num)
    if not num or tonumber(num) <= 1 then
        self:setNumBgVisible(false)
        return
    end
    self:setNumBgVisible(true)
    self._ui.num:setString(tostring(num))
end

-- 仅提供一个空方法给外部调用，关闭tips的时候会调用
function ClassRef:setSelected()
end

--pos = {x=, y=}
function ClassRef:setTipEnable(b)
    self._tipEnable = b
end

function ClassRef:setShowOrign(b)
    self._showOrign = b
end

function ClassRef:showTip(pos)
    if self._tipEnable and self._data then
        local type = self._data.itemBagType
        local data = self._data

        local param = { itemId=tonumber(data.tid) }
        local tipsType
        if type == Enums.ItemBagType.daibi then
            tipsType = Enums.TipsType.daibi
        elseif type == Enums.ItemBagType.mount then
            tipsType = Enums.TipsType.mount
            param.showPreviewBtn = true
        elseif type == Enums.ItemBagType.equip then
            tipsType = Enums.TipsType.equip
            param.showPreviewBtn = true
        elseif type == Enums.ItemBagType.jewel then
            tipsType = Enums.TipsType.jewel
        elseif type == Enums.ItemBagType.fame then -- 声望的tips当代币处理
            param.itemId = tonumber(data.uid)
            tipsType = Enums.TipsType.fame
        elseif type == Enums.ItemBagType.mountSoul then
            param.itemId = tonumber(data.id)
            tipsType = Enums.TipsType.mountSoul
        elseif type == Enums.ItemBagType.partner then
            param.itemId = tonumber(data.id)
            tipsType = Enums.TipsType.partner
            param.showPreviewBtn = true
        elseif type == Enums.ItemBagType.fashion then
            tipsType = Enums.TipsType.fashion
        elseif type == Enums.ItemBagType.mapBuff then
            tipsType = Enums.TipsType.mapBuff
        else
            tipsType = Enums.TipsType.item
        end

        param.dataTemp = data
        param.tipsType = tipsType
        param.unShowOrigin = self._showOrign or false -- 显示来源
        param.closeCallBack = function()
            -- print("IconRender.removeTip")
            -- do nothing
        end
        local boundingBoxTemp = self:getBoundingBox()
        local worldPosTemp = self:getParent():convertToWorldSpace( cc.p( boundingBoxTemp.x, boundingBoxTemp.y ) )
        boundingBoxTemp.x = worldPosTemp.x
        boundingBoxTemp.y = worldPosTemp.y
        param.boundingBox = boundingBoxTemp
        app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params=param})
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
